package core.view
{
	import core.Main;
	
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.StageVideoAvailabilityEvent;
	import flash.geom.Rectangle;
	import flash.media.SoundTransform;
	import flash.media.StageVideo;
	import flash.media.StageVideoAvailability;
	import flash.media.Video;
	
	import org.ais.event.TEvent;
	import org.ais.system.Ais;

	public class VideoView extends VUSprite
	{
		static protected var instance:VideoView;
		
		protected var _video:Video;
		protected var _stageVideo:StageVideo;
		
		protected var _sizeType:int;
		
		protected var _vWidth:Number;
		protected var _vHeight:Number;
		
		public function VideoView()
		{
			init();
		}
		
		protected function init():void
		{
			if (Ais.IMain.stage.stageVideos.length !== 0) {
				_stageVideo = Ais.IMain.stage.stageVideos[0];
				_stageVideo.attachNetStream(getNetStream());
			}
			else {
				_video = new Video();
				_video.smoothing = true;
				addChild(_video);
				_video.attachNetStream(getNetStream());
			}
			
			getNetStream().addClientEvent("onMetaData", onMetaData);
			getNetStream().addEventListener(NetStatusEvent.NET_STATUS, __netStatusHadler);
			Ais.IMain.stage.addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, __stageVideoHandler);
		}
		
		protected function onMetaData(data:*):void
		{
			_vWidth = getNetStream().getMetaData().width;
			_vHeight = getNetStream().getMetaData().height;
			
			Ais.IMain.stage.dispatchEvent(new Event(Event.RESIZE));
			data = null;
		}
		
		protected function clearVideo():void
		{
			if (null !== _stageVideo) {
				_stageVideo.viewPort = new Rectangle(0, 0, 0, 0);
				graphics.clear();
				graphics.beginFill(0x111111);
				graphics.drawRect(0, 0, Ais.IMain.stage.stageWidth, Ais.IMain.stage.stageHeight);
				graphics.endFill();
			}
			else if (null !== _video) {
				_video.clear();
			}
		}
		
		protected function __stageVideoHandler(e:StageVideoAvailabilityEvent):void
		{
			if (e.availability === StageVideoAvailability.AVAILABLE) {
				if (null === _stageVideo) {
					_stageVideo = Ais.IMain.stage.stageVideos[0];
					_stageVideo.attachNetStream(getNetStream());
				}
				if (null !== _video) {
					if (null !== _video.parent) _video.parent.removeChild(_video);
					_video.attachNetStream(null);
					_video.clear();
					_video = null;
				}
			}
			else {
				if (null === _video) {
					_video = new Video();
					_video.smoothing = true;
					addChild(_video);
					_video.attachNetStream(getNetStream());
				}
				if (null !== _stageVideo) {
					_stageVideo.attachNetStream(null);
					_stageVideo = null;
				}
			}
			e = null;
		}
		
		public function __netStatusHadler(e:NetStatusEvent):void
		{
			if (e.info.code === "NetStream.Play.Stop") {
				clearVideo();
			}
		}
		
		override public function setSize(w:Number, h:Number):void
		{
			var _vsc:Number = _vWidth / _vHeight;
			var _w:Number, _h:Number;
			switch (_sizeType) {
				case 0:
					if (_vsc > w / h) {
						_w = w;
						_h = 1 / _vsc * w;
					}
					else {
						_h = h;
						_w = _vsc * h;
					}
					break;
			}
			if (!_w) _w = 0;
			if (!_h) _h = 0;
			var _x:Number = (w - _w) * 0.5;
			var _y:Number = (h - _h) * 0.5;
			if (null !== _stageVideo) {
				_stageVideo.viewPort = new Rectangle(_x, _y, _w, _h);
			}
			else if (null !== _video) {
				_video.x = _x;
				_video.y = _y;
				_video.width = _w;
				_video.height = _h;
			}
			graphics.clear();
			if (_x !== 0 || _y !== 0) {
				graphics.beginFill(0x111111);
				graphics.drawRect(0, 0, w, (h - _h) * 0.5);
				graphics.drawRect(_x + _w, 0, (w - _w) * 0.5, h);
				graphics.drawRect(0, _y + _h, w, (h - _h) * 0.5);
				graphics.drawRect(0, 0, (w - _w) * 0.5, h);
				graphics.endFill();
			}
		}
		
		public function setSizeType(type:int):void
		{
			_sizeType = type;
		}
		
		public function setVolume(value:Number):void
		{
			var _st:SoundTransform = new SoundTransform();
			_st.volume = value;
			getNetStream().soundTransform = _st;
			_st = null;
		}
		
		public function getNetStream():*
		{
			return Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream();
		}
		
		public function getVideo():*
		{
			if (null !== _stageVideo) return _stageVideo;
			else if (null !== _video) return _video;
			return null;
		}
		
		public function play(url:String):void
		{
			clearVideo();
			if (Main.IS.isLocal === true) {
				getNetStream().isNormal = true;
				getNetStream().play(url);
				TEvent.trigger(getNetStream().NAME, "NEW_STREAM", getNetStream());
			}
			else {
				getNetStream().playURL(url);
			}
			url = null;
		}
		
		override public function clear():void
		{
//			Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.clear();
			Ais.IMain.stage.removeEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, __stageVideoHandler);
			if (null !== _stageVideo) {
				_stageVideo.attachNetStream(null);
				_stageVideo = null;
			}
			else if (null !== _video) {
				_video.attachNetStream(null);
				_video.clear();
				_video = null;
			}
			instance = null;
			super.clear();
		}
		
		static public function getInstance():VideoView
		{
			if (null === instance) instance = new VideoView();
			return instance;
		}
		
	}
}